package com.jasontools.ui;

import com.wondertek.liveroom.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.view.View.OnFocusChangeListener;

public class IconEditText extends EditText implements OnFocusChangeListener, TextWatcher {

	private Drawable leftIcon = null;
	private Drawable rightIcon = null;
	private boolean bFocused = false;
	
	private UserEnterComplete enterComplete = null;
	
	public IconEditText(Context context) {
		this(context, null);
	}
	
	public IconEditText(Context context, AttributeSet attrs) {
		this(context, attrs, android.R.attr.editTextStyle);   
	}
	
	public IconEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		this.leftIcon = this.getCompoundDrawables()[0];
		this.rightIcon = this.getCompoundDrawables()[2];
		if (this.rightIcon == null) {
			this.rightIcon = getResources().getDrawable(R.drawable.m_input_clear);
		}
		this.leftIcon.setBounds(0, 0, leftIcon.getIntrinsicWidth(), leftIcon.getIntrinsicHeight());
		this.rightIcon.setBounds(0, 0, rightIcon.getIntrinsicWidth(), rightIcon.getIntrinsicHeight());
		this.setOnFocusChangeListener(this);
		this.addTextChangedListener(this);
		this.setRightIconVisible(false);
	}
	
	public void setUserEnterComplete(UserEnterComplete userEnterComplete) {
		this.enterComplete = userEnterComplete;
	}
	
	protected void setRightIconVisible(boolean visible) {   
		Drawable right = visible ? rightIcon : null;   
		setCompoundDrawables(getCompoundDrawables()[0],   
				getCompoundDrawables()[1], right, getCompoundDrawables()[3]);   
	} 

	
	public void setShakeAnimation() {
		this.setAnimation(shakeAnimation(5));
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean touchable = false;
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if(this.rightIcon != null) {
				touchable = (event.getX() > (this.getWidth()-this.getTotalPaddingRight())) 
						&& (event.getX() < (this.getWidth()-this.getPaddingRight()));
				if(touchable) {
					this.setText(null);
				}
			}
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onTextChanged(CharSequence s, int start, int count,
			int after) {
		if (this.bFocused) {
			this.setRightIconVisible(s.length()>0);
			if(this.enterComplete!= null) {
				enterComplete.changeButtonStatus(s.length() > 0);
			}
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		this.bFocused = hasFocus;
		if(this.bFocused) {
			this.setRightIconVisible(this.getText().toString().trim().length()>0);
		} else {
			this.setRightIconVisible(false);
		}
	}
	
	public static Animation shakeAnimation(int counts){  
		Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);  
		translateAnimation.setInterpolator(new CycleInterpolator(counts));  
		translateAnimation.setDuration(1000);  
		return translateAnimation;  
	} 
}



